package com.promise.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.promise.controller.result.DutyResult;
import com.promise.pojo.Duty;
import com.promise.pojo.Warning;
import com.promise.service.DutyServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/dutys")
public class DutyController {
    @Autowired
    private DutyServiceImpl dutyService;

    /**
     * 分页查询用户
     *
     * @param currentPage 当前页码
     * @param pageSize    页面数据条数
     * @return UserResult
     */
    @PostMapping("/{currentPage}/{pageSize}")
    @ResponseBody
    public DutyResult getDutyByPage(@PathVariable Integer currentPage, @PathVariable Integer pageSize,
                                    @RequestBody Duty searchData) {
        // 查询条件(值班人，联系电话)
        LambdaQueryWrapper<Duty> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(searchData.getDutyDate()),
                        Duty::getDutyDate, searchData.getDutyDate())
                .eq(!StringUtils.isEmpty(searchData.getWatchName()),
                        Duty::getWatchName, searchData.getWatchName());;

        IPage<Duty> iPage = new Page<>(currentPage, pageSize);
        IPage<Duty> page = dutyService.page(iPage, queryWrapper);
        Integer code = page.getRecords() != null ? Code.SUCCESS_CODE : Code.ERROR_CODE;
        String msg = page.getRecords() != null ? "查询成功" : "查询失败";
        Map<String, Object> data = new HashMap<>();
        data.put("pages", page.getPages());
        data.put("currentPage", page.getCurrent());
        data.put("pageSize", page.getSize());
        data.put("dutys", page.getRecords());
        data.put("total", page.getTotal());
        return new DutyResult(code, data, msg);
    }

    /**
     * 添加值班信息
     *
     * @param dutyDto 要添加的值班信息对象
     * @return DutyResult
     */
    @PostMapping("/add")
    @ResponseBody
    public DutyResult add(@RequestBody Duty dutyDto) {
        boolean flag = dutyService.save(dutyDto);
        Integer code = flag ? Code.SUCCESS_CODE : Code.ERROR_CODE;
        String msg = flag ? "添加成功" : "添加失败";
        Map<String, Object> data = new HashMap<>();
        data.put("duty", dutyDto);
        return new DutyResult(code, data, msg);
    }


    /**
     * 根据dutyId删除用户
     *
     * @param dutyId 用户的id
     * @return DutyResult
     */
    @DeleteMapping("delete/{dutyId}")
    @ResponseBody
    public DutyResult delete(@PathVariable Integer dutyId) {
        boolean flag = dutyService.removeById(dutyId);
        Integer code = flag ? Code.SUCCESS_CODE : Code.ERROR_CODE;
        String msg = flag ? "删除成功" : "删除失败";
        return new DutyResult(code, flag, msg);
    }


    /**
     * 根据dutyId修改值班信息
     *
     * @param dutyId  要修改的值班信息的ID
     * @param dutyDto 包含新值班信息的对象
     * @return DutyResult
     */
    @PutMapping("update/{dutyId}")
    @ResponseBody
    public DutyResult update(@PathVariable Integer dutyId, @RequestBody Duty dutyDto) {
        // 检查传入的dutyDto是否有有效的ID，且与路径变量dutyId相匹配
        if (dutyDto.getDutyId() == null || dutyDto.getDutyId() != dutyId) {
            return new DutyResult(Code.ERROR_CODE, null, "更新失败，提供的ID不匹配");
        }

        // 调用服务层的updateById方法来更新记录
        boolean isSuccess = dutyService.updateById(dutyDto);

        // 根据更新操作的成功与否构造DutyResult对象
        Map<String, Object> dataContainer = new HashMap<>();
        DutyResult result;
        if (isSuccess) {
            dataContainer.put("duty", dutyDto);
            result = new DutyResult(Code.SUCCESS_CODE, dataContainer, "更新成功");
        } else {
            result = new DutyResult(Code.ERROR_CODE, null, "更新失败");
        }
        return result;
    }
}
